package com.sense.feel.login.manager

import com.facebook.FacebookSdk.getApplicationContext
import com.linecorp.linesdk.api.LineApiClient
import com.linecorp.linesdk.api.LineApiClientBuilder
import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import com.linecorp.linesdk.auth.LineLoginApi
import com.linecorp.linesdk.auth.LineAuthenticationParams
import com.sense.kukoo.lib_logger.LoggerManager
import com.sense.feel.login.manager.callback.OnLineLoginCallback
import com.linecorp.linesdk.LineApiResponseCode
import com.linecorp.linesdk.Scope
import java.lang.Exception
import java.util.*
import java.util.concurrent.Executors

object LineLoginManager {
    const val LINE_REQUEST_CODE = 9002
    private var lineApiClient: LineApiClient? = null
    private const val TAG = "LineLoginManager"
    private const val channelId = "1657113301"
    private var token: String? = null
    fun initLine() {
        val apiClientBuilder = LineApiClientBuilder(getApplicationContext(), channelId)
        lineApiClient = apiClientBuilder.build()
    }

    fun lineLogin(activity: Activity) {
        try {
            val loginIntent = LineLoginApi.getLoginIntent(
                activity,
                channelId,
                LineAuthenticationParams.Builder()
                    .scopes(Arrays.asList(Scope.PROFILE))
                    .build()
            )
            activity.startActivityForResult(loginIntent, LINE_REQUEST_CODE)
        } catch (e: Exception) {
            LoggerManager.e("ERROR", e.toString())
        }
    }

    fun lineLoginCallBack(
        requestCode: Int,
        data: Intent?,
        onLineLoginCallback: OnLineLoginCallback
    ) {
        if (requestCode != LINE_REQUEST_CODE) {
            LoggerManager.e(TAG, "Unsupported Request")
            return
        }
        val result = LineLoginApi.getLoginResultFromIntent(data)
        when (result.responseCode) {
            LineApiResponseCode.SUCCESS -> {
                val accessToken = result.lineCredential!!.accessToken.tokenString
                token = accessToken
                LoggerManager.e(TAG, "accessToken=$accessToken")
                LoggerManager.e(TAG, "line_profile=" + result.lineProfile.toString())
                LoggerManager.e(TAG, "line_credential=" + result.lineCredential)
                onLineLoginCallback.onSuccess(result.lineProfile!!)
            }
            LineApiResponseCode.CANCEL -> {
                LoggerManager.e("ERROR", "LINE Login Canceled by user.")
                onLineLoginCallback.onCancel()
            }
            else -> {
                LoggerManager.e("ERROR", "Login FAILED!")
                LoggerManager.e("ERROR", result.errorData.toString())
                onLineLoginCallback.onFail(result.errorData.toString())
            }
        }
    }

    fun loginOut() {
        if (lineApiClient != null && !TextUtils.isEmpty(token)) {
            Executors.newSingleThreadExecutor().submit { lineApiClient!!.logout() }
        }
    }
}